package com.easy.controller;

import com.alibaba.excel.EasyExcel;
import com.easy.bean.Cases;
import com.easy.bean.Patient;
import com.easy.common.CommonResult;
import com.easy.dao.IPatientDao;
import com.easy.excel.EasyObjExcelListener;
import com.easy.service.IPatientService;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

@RestController
public class PatientController {
    @Autowired
    IPatientDao dao;

    @Autowired
    IPatientService paS;

    @GetMapping("patient")
    public CommonResult getPatient(Patient patient, Page page){
        List<Patient> list=paS.getPatient(patient,page);
        int count=paS.getCount(patient);
        HashMap map = new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }
    @PostMapping("patient")
    public CommonResult  addPatientJSON(@RequestBody Patient patient){
        paS.addPatient(patient);
        patient=paS.getPatientById(patient.getId());
        return CommonResult.success(patient);
    }

    @DeleteMapping("patient/{id}")
    public CommonResult delPatient(@PathVariable int id){
        int count=dao.delPatient(id);
        if(count>0){
            return CommonResult.success();
        }else {
            return CommonResult.fail();
        }
    }

    @PutMapping("patient")
    public CommonResult editPatientJSON(@RequestBody Patient patient){
        paS.editPatient(patient);
        patient=paS.getPatientById(patient.getId());
        return CommonResult.success(patient);
    }


//    @PostMapping("patientxcel")
//    public CommonResult patientExcel(@RequestBody @RequestParam("file") MultipartFile file){
//        EasyObjExcelListener<Patient> patientList= new EasyObjExcelListener<Patient>();
//       InputStream fis=null;
//    try {
//        fis = file.getInputStream();
//    }catch (IOException e){
//        e.printStackTrace();
//    }
//    EasyExcel.read(fis,Patient.class,patientList).sheet(0).doRead();
//    return CommonResult.success(patientList.list);
//    }



    @PostMapping("patientexcel")
    public CommonResult patientExcel( @RequestParam("file")MultipartFile file){
        EasyObjExcelListener<Patient> patientList= null;
        try{
            patientList = new EasyObjExcelListener<Patient>();
            InputStream fis= null;
            try {
                fis = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();

            }
            EasyExcel.read(fis,Patient.class,patientList).sheet(0).doRead();
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.fail(400,"解析失败");
        }

        return CommonResult.success(200,"Excel导入成功",patientList.list);

    }



}
